﻿using System.Collections.Generic;
using System.Text;

namespace tiny_compiler
{
	public class Print :
		IOperator
	{
		public Print(string _valueName)
		{
			m_valueName = _valueName;
		}

		#region IOperator Members

		public int Length
		{
			get { return Compiler.NameLength + Compiler.LengthLength + (Compiler.NumberLength * 2); }
		}

		public string Name
		{
			get { return string.Empty; }
		}

		public void Resolve(IEnumerable<IOperator> _operators)
		{
			m_sourceOffset = _operators.Offset(m_valueName);
			var op = _operators.Operator(m_valueName);
			m_sourceLength = op.Length;
		}

		public void Append(StringBuilder _data)
		{
			_data.Append(Compiler.PrintName);
			_data.Append(CompilerUtility.ConvertLength(Length));
			_data.Append(CompilerUtility.ConvertNumber(m_sourceOffset));
			_data.Append(CompilerUtility.ConvertNumber(m_sourceLength));
		}

		#endregion

		private string m_valueName;
		private int m_sourceOffset;
		private int m_sourceLength;
	}
}
